package com.leetcode.first50;

/**
 * Created by weishaofei on 2017/10/26.
 */
public class AddTwoNumbers {
    //     Definition for singly-linked list.
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode node1, ListNode node2) {
        int val = node1.val + node2.val;
        if (node1.next == null) {
            if (val < 10) {
                return node1;
            } else {
                ListNode next = new ListNode(val % 10);
                node1.next = next;
                return node1;
            }
        }
        int shang = 0;
        ListNode last = new ListNode(0);
        while (node1.next != null) {
            val = node1.val + node2.val + shang;
            node1.val = val % 10;
            shang = val / 10;
            node1 = node1.next;
            node2 = node2.next;
        }

        return node1;
    }

    public static void main(String[] args) {

    }
}
